const { getproduct, setproductstatus, getuser, getprice } = require('../../service/main/product')
module.exports = {
    async getorder(ctx, next) {
        const rudata = await getproduct()
        ctx.body = {
            code: 200,
            msg: '获取成功',
            data: rudata
        }
    },
    async setproductstatus(ctx, next) {
        const { id, status } = ctx.request.body
        const rudata = await setproductstatus(id, status)
        ctx.body = {
            code: 200,
            msg: '获取成功',
            data: rudata
        }
    },
    async getuserandpri(ctx, next) {
        try {
            const userdata = await getuser();
            if (!userdata.length) {
                ctx.body = { code: 200, msg: '无用户数据', data: [] };
                return;
            }
            const responesdata = await Promise.all(
                userdata.map(async (user) => {
                    const price = await getprice(user.id);
                    return { ...user.dataValues, price };
                })
            );

            ctx.body = {
                code: 200,
                msg: '获取成功',
                data: responesdata
            };
        } catch (error) {
            console.error('获取用户及价格失败:', error);
            ctx.body = {
                code: 500,
                msg: '服务器内部错误',
                data: []
            };
        }
    }
}